// pages/addAddress/index.js
const App=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: '',
    addressId:null,
    disabled:false,
    province_list:null,
    province_name:null,
    city_list:null,
    city_name:null,
    area_list:null,
    area_name:null,
    addressCity:null,
    multiArray: [],  // 三维数组数据
    multiIndex: [0, 0, 0], // 默认的下标,
    selectProvinceId:null,
    selectCityId:null,
    selectAreaId:null,
  },
  onLoad(options){
    let addressId=options.id
    if(addressId){
      this.setData({ addressId})
      this.getAddressDetail(options.id)
    }
    this.getProvince()
  },
//获取地址详情
  getAddressDetail(id){
    App._get('User/Api002', {id}, (data) => {
      let addressInfo=data
          addressInfo.addressCity=[data.provinceTitle,data.cityTitle,data.countyTitle]
      this.setData(addressInfo)
    })
  },
  //表单提交
  saveData(e) {
    let _this = this,
      values = e.detail.value
    values.region = _this.data.region;
    // 记录formId
    // App.saveFormId(e.detail.formId);
    // 表单验证
    if (!_this.validation(values)) {
      App.showError(_this.data.error);
      return false;
    }
    // 按钮禁用
    _this.setData({
      disabled: true
    });
    let dataObj={
      linkName:values.name,
      id:this.addressId||'0',
      linkTel:values.phone,
      provinceID:this.data.selectProvinceId,
      cityID:this.data.selectCityId,
      countyID:this.data.selectAreaId,
      address:values.detail,
      isDefault:values.isDefault?'1':'0'
    }
    // 提交到后端
    App._post_form('User/Api003', dataObj, function(result) {
      App.showSuccess('添加成功', function() {
        wx.navigateBack();
      });
    }, false, function() {
      // 解除禁用
      _this.setData({
        disabled: false
      });
    });
  },
  //表单验证
  validation(values) {
    console.log(values)
    if (values.name === '') {
      this.data.error = '收货人不能为空';
      return false;
    }
    if (values.phone.length < 1) {
      this.data.error = '手机号不能为空';
      return false;
    }
    // if (values.phone.length !== 11) {
    //   this.data.error = '手机号长度有误';
    //   return false;
    // }
    let reg = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/;
    if (!reg.test(values.phone)) {
      this.data.error = '手机号不符合要求';
      return false;
    }
    if (!this.data.addressCity) {
      this.data.error = '省市区不能空';
      return false;
    }
    if (values.detail === '') {
      this.data.error = '详细地址不能为空';
      return false;
    }
    return true;
  },
//获取省份列表
  getProvince(){
    let that = this
    App._get('Site/Api005',{},res=>{
      let province_list = [...res.items]  //放到一个数组里面
      let province_name = res.items.map((item)=>{return item.title})  //获取名称
      that.setData({
        multiArray:[province_name,[],[]], //更新三维数组  更新完为[['广东','北京'],[],[]]
        province_list,  //省级原始数据
        province_name,  //省级全部名称
      })
      let defaultCode = that.data.province_list[0].id //使用第一项当作参数获取市级数据
      if(defaultCode){
        that.setData({
          currnetProvinceId:defaultCode  //保存当前省份id
        })
        that.getCity(defaultCode) //获取市区数据
      }
    })
  },
//根据省份id获取城市
  getCity(id){
    let that = this
    that.setData({
      currnetProvinceId: id
    })
    App._get('Site/Api006',{id},res=>{
      let cityArr = res.items.map((item)=>{return item.title}) //返回城市名称
      let cityList = [...res.items]
      that.setData({
        multiArray:[that.data.province_name,cityArr,[]],  //更新后[['广东','北京'],['潮州'，'汕头','揭阳'],[]]
        city_list:cityList, //保持市级数据
        city_name:cityArr   //市级名称
      })
      let defaultCode = that.data.city_list[0].id //获取第一个市的区级数据
      if(defaultCode){
        that.setData({
          currentCityId:defaultCode //保存当下市id
        })
        that.getArea(defaultCode) //获取区域数据
      }
    })
  },
//获取区域
  getArea(id){
    let that = this
    that.setData({
      currentCityId:id    //保存当前选择市
    })
    App._get('Site/Api006',{id},res=>{
      let areaList = [...res.items]
      let areaArr = res.items.map((item)=>{return item.title}) //区域名
      that.setData({
        multiArray:[that.data.province_name,that.data.city_name,areaArr],
        area_list:areaList, //区列表
        area_name:areaArr   //区名字
      })
    })
  },
  //picker确认选择地区
  bindRegionChange(e){
    // 因为在获取省中 北京只有一个选项，导致获取不了北京》北京》第一个
    if(e.detail.value[1]==null || e.detail.value[2]==null){ //如果只滚动了第一列则选取第一列的第一数据
      this.setData({
        multiIndex: e.detail.value,  //更新下标
        addressCity: [this.data.province_list[e.detail.value[0]].title, this.data.city_list[0].title, this.data.area_list[0].title],
        selectProvinceId: this.data.province_list[e.detail.value[0]].id,
        selectCityId: this.data.city_list[0].id,
        selectAreaId: this.data.area_list[0].id
      })
    }else{

      this.setData({
        multiIndex: e.detail.value,  //更新下标
        addressCity: [this.data.province_list[e.detail.value[0]].title, this.data.city_list[e.detail.value[1]].title, this.data.area_list[e.detail.value[2]].title],
        selectProvinceId: this.data.province_list[e.detail.value[0]].id,
        selectCityId: this.data.city_list[e.detail.value[1]].id,
        selectAreaId: this.data.area_list[e.detail.value[2]].id
      })

    }
    console.log(e.detail.value,this.data.addressCity)
    console.log(this.data.selectProvinceId,this.data.selectCityId,this.data.selectAreaId)
  },
  //滑动地区组件
  bindRegionColumnChange(e){
    // console.log(e.detail.column,e.detail.value)
    let that = this
    let column = e.detail.column  //当前改变的列
    let data = {
      multiIndex:JSON.parse(JSON.stringify(that.data.multiIndex)),
      multiArray:JSON.parse(JSON.stringify(that.data.multiArray))
    }
    data.multiIndex[column] = e.detail.value  //第几列改变了就是对应multiIndex的第几个，更新他
    switch(column){
      case 0 :  //第一列改变，省级改变
        let currentProvinceId = that.data.province_list[e.detail.value].id
        if(currentProvinceId != that.data.currentProvinceId){ //判断当前id是不是更新了
          that.getCity(currentProvinceId)   //获取当前id下的市级数据
        }
        data.multiIndex[1] = 0    //将市默认选择第一个
        break
      case 1 :  //第二列改变，市级改变
        let currentCityId = that.data.city_list[e.detail.value].id
        if(currentCityId != that.data.currentCityId){
          that.getArea(currentCityId) //获取区域
        }
        data.multiIndex[2] = 0  //区域默认第一个
        break
    }
    that.setData(data)  //更新数据
  },
})